﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.Model;
using System.Data;
using HZ.DrugTrade.IBLL;

namespace HZ.DrugTrade.Web.Admin.SystemManage
{
    public partial class ChildUserOperateBatch : BasePage
    {
        IBLL_User iu = Factory.BLL_User();

        //保存操作集合 以备二级绑定时使用
        DataTable _mianUserOperateTable;

        public ChildUserOperateBatch()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.子用户管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string userIds = Request.QueryString["uids"];
            string parentId = Request.QueryString["pid"];
            if (!this.IsPostBack)
            {
                //
                ViewState["v_uids"] = userIds;
                ViewState["v_pid"] = parentId;

                if (!string.IsNullOrEmpty(userIds))
                {
                    lblUserId.Text = userIds;
                    if (!string.IsNullOrEmpty(parentId))
                    {

                        //用户上级主用户的操作权限
                        IList<OperatingRule> mainUserOperateList = iu.GetUserOperatingRule(parentId);

                        //转换成DataTable
                        _mianUserOperateTable = HZ.Data.ModelConvert.GetDataTableFromModel(mainUserOperateList);

                        //分组以获得模块列表
                        DataTable tableModule = _mianUserOperateTable.DefaultView.ToTable(true, "Module");
                        //添加模块枚举值列
                        tableModule.Columns.Add(new DataColumn("ModuleValue", typeof(System.Int32)));
                        foreach (DataRow row in tableModule.Rows)
                        {
                            row["ModuleValue"] = EnumParse.GetModule((int)row["Module"]);
                        }

                        //按模块枚举值排序
                        DataView dv = new DataView(tableModule);
                        dv.Sort = "ModuleValue asc";

                        this.rptModule.DataSource = dv;
                        this.rptModule.DataBind();
                    }
                    else
                    {
                        PageError("主用户不存在", "ChildUserList.aspx");
                    }
                }
                else
                {
                    PageError("用户不存在", "ChildUserList.aspx");
                }
            }
        }

        /// <summary>
        /// 绑定rptOperate
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataTable table;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                table = this._mianUserOperateTable;
                if (table.Rows.Count > 0)
                {
                    DataView dv = new DataView(table);
                    dv.RowFilter = "Module ='" + DataBinder.Eval(e.Item.DataItem, "Module").ToString() + "'";
                    //dv.Sort = " OperatingRuleName";
                    Repeater rptStoreSubject = (Repeater)e.Item.FindControl("rptOperate");
                    rptStoreSubject.DataSource = dv;
                    rptStoreSubject.DataBind();
                }
            }
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strSucessUserIds = "";
            string strErrorUserIds = "";

            string userIds = ViewState["v_uids"].ToString();
            string parentId = ViewState["v_pid"].ToString();


            //构造权限集合
            List<OperatingRule> operatingRuleList = new List<OperatingRule>();
            string ids = Request["chkOperate"];
            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    OperatingRule otr = new OperatingRule();
                    otr.OperatingRuleID = Item;
                    operatingRuleList.Add(otr);
                }
            }

            //循环保存
            string[] arrayUids = userIds.Split(',');
            foreach (string Item in arrayUids)
            {
                if (iu.SetUserOperatingRule(Item, operatingRuleList))
                {
                    strSucessUserIds += Item + ",";
                }
                else
                {
                    strErrorUserIds += Item + ",";
                }
            }

            string strMessage = "权限更新成功执行！结果如下：\n\n";
            strMessage += "成功的用户：" + strSucessUserIds + "。\n";
            strMessage += "失败的用户：" + strErrorUserIds + "。";
            //显示结果
            PageInfo(strMessage, this.ReturnUrl);

        }


    }
}
